// pages/upload/upload.js
/**页面跳转前 将参数放在js中 */
var util = require('../../utils/util');
var app = getApp();//引入全局变量
Page({
  /**
   * 页面的初始数据
   */
  data: {
    //本地数据
    head_Photo: wx.getStorageSync('user').head_Photo,//头像
    names: wx.getStorageSync('user').userName,//userName 放用户名
    userId: wx.getStorageSync('user').userId,
    talks: [],
    talks_dy: [],
    inputValue: '',
    inputValue_dy: '',
    imgUrl:null,//保存拼接起来的url并上传
  },
  user: null,
  /**选择图片预览在框中 */
  upload_editor: null,
  img:null,
  flag_isclick:null,
  selectImageUpLoad:function () {
    
     this.chooseImageTap()
  },

  chooseImageTap:function(){
    var that = this;
    wx.showActionSheet({
      itemList: ['从相册选择','拍照'],
      itemColor:"#000000",
      success:function(res){
        if(!res.cancel){
          if(res.tapIndex == 0){
            that.chooseWxImage('album')
          }else if(res.tapIndex ==1){
            that.chooseWxImage('camera')
          }
        }
      }
    })
  },
  //图片本地路径
  chooseWxImage:function(type){
    var that = this;
    var imgsPaths = that.data.imgs;
    wx.chooseImage({
      sizeType:['original','compressed'],
      sourceType:[type],
      success:function(res){
        console.log(res.tempFilePaths[0]);//返回的路径
        that.upImgs(res.tempFilePaths[0],0);//调用上传方法
        that.setData({
          ImagePath:res.tempFilePaths[0]
        })
      }
    })
  },
  //上传服务器
  upImgs:function(imgurl,index){
    var that = this;
    wx.uploadFile({
      filePath: imgurl,
      name: 'image',
      url: 'https://api.lzjlw.top:3030/residential/upload',
      header:{
        'content-type':'multipart/form-data'
      },
      success:function(res){
        console.log(res)
        that.data.imgUrl = `${app.globalData.url}`+res.data
        console.log(that.data.imgUrl)
      }
    })
  },
  //删除图片
  deleteImg:function(){
    var that = this
    wx.showModal({
      cancelColor: '#000000',
      title:'提示',
      content:'是否删除',
      success:function(res){
        if(res.confirm){
          that.setData({
            ImagePath:null
          })
          that.data.imgUrl = null
          console.log(that.data.imgUrl)
        }else if(res.cancel){
          console.log('用户点击取消')
        }
      }
    })
  },

  
  

  /**发布动态 */
  uploadDymaic: function (e) {
    //app.talks app.inputValue是全局参数
    console.log(this.data.inputValue)
    let that = this;//that 是整个页面
    app.inputValue_dy = that.data.inputValue
    // console.log(app.inputValue_dy + '动态inputValue获取到了')
    // this.data.userId = wx.getStorageSync('user').userId
    that.data.talks.unshift({
      userId: this.data.userId,
      head_Photo: this.data.head_Photo,
      userName: this.data.names,
      content: app.inputValue_dy,
      public_Time: util.formatTime(new Date())
    })
    app.talks_dy = that.data.talks;//将保存了自己上传的信息的数组存到全局参数talks_dy中
    console.log(app.talks_dy)
    console.log(this.data.userId)
    console.log(this.data.imgUrl)


    //将自己发布的动态数据上传到服务器
    wx.request({
      url: 'https://api.lzjlw.top:3030/residential/community_dynamic?type=3',
      method: 'POST',
      data: {
        userId: wx.getStorageSync('user').userId,
        content: app.inputValue_dy,
        imgUrl: this.data.imgUrl,
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log(res.data)
        console.log('我的动态信息上传成功')
      }
    })
    
    //刷新上一页的数据
    const pages = getCurrentPages();
    const perpage = pages[pages.length-2]
    // perpage.onUnload();
    perpage.onLoad();

    wx.reLaunch({
      url: '/pages/community/community',
    })

  },

  //输入框失去焦点时触发
  bindInputBlur: function (e) {
    console.log(e)
    this.data.inputValue = e.detail.value;
    console.log(this.data.inputValue + 'imbind')
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.user = wx.getStorageSync('user')
    console.log(this.user)

    let query = wx.createSelectorQuery()
    query.select('#upload_preImg').context(res => {
      this.upload_preImg = res.context
      console.log(this.upload_preImg)
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    console.log(util.formatTime(new Date()))
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})